package com.tanxiaoyao.wecon.ui;

import java.util.List;

import com.tanxiaoyao.wecon.R;
import com.tanxiaoyao.wecon.app.AppManager;
import com.tanxiaoyao.wecon.net.GlobalConnectHelper;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 手机界面类
 *
 * @author tanxiaoyao (junlidev@gmail.com)
 * @version 2014/8/28 21:29.
 */
public class ActivityPowerPoint extends Activity {

	// DEBUG标志
	private static final String TAG = "tanxiaoyao-ppt";
	private static final Boolean ISDEBUG = false;
	// 定义发送信息
	private final String LAST_PAGE = "last_page";
	private final String MARK_BTN_DOWN = "mark_btn_down";
	private final String MARK_BTN_UP = "mark_btn_up";
	private final String NEXT_PAGE = "next_page";
	private final String ENTER_PPT = "enter_ppt";
	private final String EXIT_PPT = "exit_ppt";
	// 声明控件
	private Button lastPageBtn, markBtn, nextPageBtn, enterPPTBtn, exitPPTBtn;
	// 全局发送器
	// TODO 这里要对不存在等异常情况做判断
	GlobalConnectHelper globalConnectHelper = GlobalConnectHelper
			.getGlobalConnector();
	// 声明传感器管理器
	private SensorManager sensorManager;
	// 当前音量缓存
	private int curVolume;
	// 上一次音量缓存
	private int lastVolume;
	// 音量管理器
	public AudioManager audioManager;
	// 音量最大值
	private int maxVolume;
	// 监听线程
	private Thread volumeChangeThread;
	// 需要监听标志
	private boolean needListen = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//      Activity压栈
      AppManager.getAppManager().addActivity(this);
		if (ISDEBUG)
			Log.i(TAG, "Phone onCreate");
		setContentView(R.layout.activity_powerpoint);
		// 得到标题栏View对象
		View ppt_header = findViewById(R.id.no_btn_header);
		// 设置标题栏文字
		TextView ppt_header_title = (TextView) findViewById(R.id.no_btn_header_title);
		ppt_header_title.setText(R.string.powerpoint_show);
		// 设置界面的声音属性为音乐
		setVolumeControlStream(AudioManager.STREAM_MUSIC);
		// 保持手机不休眠

		// 初始化按钮
		lastPageBtn = (Button) findViewById(R.id.btn_last_page);
		markBtn = (Button) findViewById(R.id.btn_mark);
		nextPageBtn = (Button) findViewById(R.id.btn_next_page);
		enterPPTBtn = (Button) findViewById(R.id.btn_enter_powerpoint);
		exitPPTBtn = (Button) findViewById(R.id.btn_exit_powerpoint);
		// 初始化传感器管理器
		sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		// 判断是否存在重力传感器和方向传感器
		if (sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) == null
				&& sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY) == null) {
			Toast.makeText(this, getString(R.string.has_no_related_ssensors),
					Toast.LENGTH_LONG).show();
		} else {
			// 拥有相关传感器，开启按钮监听器
			// 可视化按钮
			//markBtn.setVisibility(View.VISIBLE);
			// TODO 初始化传感器，置标志位
			//markBtn.setOnTouchListener(markBtnOnTouchListener);
		}
		// 绑定按钮监听器
		lastPageBtn.setOnClickListener(btnOnClickListener);
		nextPageBtn.setOnClickListener(btnOnClickListener);
		enterPPTBtn.setOnClickListener(btnOnClickListener);
		exitPPTBtn.setOnClickListener(btnOnClickListener);
		// 获取音量管理器
		audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
		// 初始化音量最大值
		maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		// 初始化缓存值
		curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
		lastVolume = curVolume;
	}

	// 常规按钮监听器
	OnClickListener btnOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) { // 直接发送对应消息
			case R.id.btn_last_page:
				globalConnectHelper.sendMess(LAST_PAGE);
				break;
			case R.id.btn_next_page:
				globalConnectHelper.sendMess(NEXT_PAGE);
				break;
			case R.id.btn_enter_powerpoint:
				globalConnectHelper.sendMess(ENTER_PPT);
				break;
			case R.id.btn_exit_powerpoint:
				globalConnectHelper.sendMess(EXIT_PPT);
				break;

			default:
				break;
			}

		}
	};

	/**
	 * 标记按钮触摸响应
	 */
	OnTouchListener markBtnOnTouchListener = new OnTouchListener() {

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			// 同样发送对应消息
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				globalConnectHelper.sendMess(MARK_BTN_DOWN);
				break;
			case MotionEvent.ACTION_UP:
				globalConnectHelper.sendMess(MARK_BTN_UP);
				break;
			}
			return false;
		}
	};

	/**
	 * 音量键翻页 这里有点是锁屏之后可以响应
	 */

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_VOLUME_UP:
			// 发送上一页
			globalConnectHelper.sendMess(LAST_PAGE);
			// 屏蔽其他处理
			return true;
		case KeyEvent.KEYCODE_VOLUME_DOWN:
			// 发送下一页
			globalConnectHelper.sendMess(NEXT_PAGE);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	/**
	 * 锁屏时翻页键响应监听
	 */
	public void listenVolume() {
		volumeChangeThread = new Thread() {

			@Override
			public void run() {
				while (needListen) {
					// 首先休眠100ms，防止响应过快
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						if (ISDEBUG)
							Log.d(TAG, "volume listen thread sleep error!");
						e.printStackTrace();
					}

					// 然后判断当前音量值是否在可变化范围内
					String temp;
					curVolume = audioManager
							.getStreamVolume(AudioManager.STREAM_VOICE_CALL);
					temp = String.valueOf(curVolume) + "\t";
					curVolume = audioManager
							.getStreamVolume(AudioManager.STREAM_ALARM);
					temp += String.valueOf(curVolume) + "\t";
					curVolume = audioManager
							.getStreamVolume(AudioManager.STREAM_DTMF);
					temp += String.valueOf(curVolume) + "\t";
					curVolume = audioManager
							.getStreamVolume(AudioManager.STREAM_NOTIFICATION);
					temp += String.valueOf(curVolume) + "\t";
					curVolume = audioManager
							.getStreamVolume(AudioManager.STREAM_RING);
					temp += String.valueOf(curVolume) + "\t";
					curVolume = audioManager
							.getStreamVolume(AudioManager.STREAM_SYSTEM);
					temp += String.valueOf(curVolume) + "\t";
					curVolume = audioManager
							.getStreamVolume(AudioManager.STREAM_MUSIC);
					temp += String.valueOf(curVolume);
					if (ISDEBUG)
						Log.d(TAG, "STREAM: " + temp);

					if (ISDEBUG)
						Log.d(TAG, "curVolume: " + curVolume);
					if (ISDEBUG)
						Log.d(TAG, "lastVolume: " + lastVolume);
					if (curVolume == 0 || curVolume == maxVolume) {
						// 置为中间值
						audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
								maxVolume / 4,
								AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
						lastVolume = maxVolume / 4;
						if (ISDEBUG)
							Log.d(TAG, "volume reset: " + lastVolume);
					}
					// 判断音量变化
					if (curVolume > lastVolume) {
						// 发送上翻页
						globalConnectHelper.sendMess(LAST_PAGE);
						// 重置音量
						audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
								lastVolume,
								AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
					} else if (curVolume < lastVolume) {
						// 发送上翻页
						globalConnectHelper.sendMess(NEXT_PAGE);
						// 重置音量
						audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
								lastVolume,
								AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
					}
				}
			}

		};

	}

}
